using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Pool;
using UnityEngine.UI;

namespace GameFrameWork
{
    /// <summary>
    ///   <para>Adds an outline to a graphic using IVertexModifier.</para>
    /// </summary>
    [AddComponentMenu("UGUI/Effects/Outline")]
    public class UITextOutline : UGUIShadow
    {
        [SerializeField]
        private bool diagonal = true;

        [SerializeField]
        private bool highQualityOutline = true;

        protected UITextOutline()
        {
        }

        public override void ModifyMesh(VertexHelper vh)
        {
            if (!this.IsActive()) { return; }

            List<UIVertex> list = ListPool<UIVertex>.Get();
            vh.GetUIVertexStream(list);
            CutUIVertexList(list);

            int num = list.Count * 5;
            if (list.Capacity < num)
            {
                list.Capacity = num;
            }

            int start;
            int count = 0;

            if (diagonal || highQualityOutline)
            {
                start = 0;
                count = list.Count;
                base.ApplyShadowZeroAlloc(list, base.EffectColor, start, list.Count, +base.EffectDistance.x, +base.EffectDistance.y);

                start = count;
                count = list.Count;
                base.ApplyShadowZeroAlloc(list, base.EffectColor, start, list.Count, +base.EffectDistance.x, -base.EffectDistance.y);

                start = count;
                count = list.Count;
                base.ApplyShadowZeroAlloc(list, base.EffectColor, start, list.Count, -base.EffectDistance.x, +base.EffectDistance.y);

                start = count;
                count = list.Count;
                base.ApplyShadowZeroAlloc(list, base.EffectColor, start, list.Count, -base.EffectDistance.x, -base.EffectDistance.y);
            }

            if (!diagonal || highQualityOutline)
            {
                start = count;
                count = list.Count;
                base.ApplyShadowZeroAlloc(list, base.EffectColor, start, list.Count, 0, -base.EffectDistance.y);

                start = count;
                count = list.Count;
                base.ApplyShadowZeroAlloc(list, base.EffectColor, start, list.Count, 0, +base.EffectDistance.y);

                start = count;
                count = list.Count;
                base.ApplyShadowZeroAlloc(list, base.EffectColor, start, list.Count, -base.EffectDistance.x, 0);

                start = count;
                count = list.Count;
                base.ApplyShadowZeroAlloc(list, base.EffectColor, start, list.Count, +base.EffectDistance.x, 0);
            }

            vh.Clear();
            List<int> indices = GetIndices(list);
            vh.AddUIVertexStream(list, indices);

            ListPool<UIVertex>.Release(list);
        }
    }
}

